﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Role
{
    public partial class RoleList : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("JSGL");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                InitBind();
            }
            btnCancle.Attributes.Add("OnClick", "return confirm('你确定要删除？');");
        }

        /// <summary>
        /// 从数据库中查出所有角色的信息
        /// </summary>
        private void InitBind()
        {
            LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable();
            gvRole.DataSource = BRT.GetAllList().Tables[0].DefaultView;
            gvRole.DataBind();
        }

        /// <summary>
        /// 增加角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void btnAddRole_Click(object sender, EventArgs e)
        //{
        //    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>window.open('Add.aspx','','top=200,left=250,height=400,width=400');</script>");

        //}

        protected void gvRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");

                int item = e.Row.Cells.Count;
                for (int i = 0; i < item; i++)
                {
                    //e.Row.Cells[i].Attributes.Add("OnDblClick", "window.open('Edit.aspx?RoleId=" + gvRole.DataKeys[e.Row.DataItemIndex].Value.ToString() + "','newwindow','top=200,left=250,height=400,width=400')");
                    e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='Edit.aspx?RoleId=" + gvRole.DataKeys[gvRole.DataKeys.Count - 1].Value.ToString() + "'");
                }

                e.Row.Attributes["style"] = "Cursor:pointer";
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancle_Click(object sender, EventArgs e)
        {
            LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable();
            bool success = false;
            CheckBox chk;
            for (int i = 0; i < gvRole.Rows.Count; i++)
            {
                chk = ((CheckBox)gvRole.Rows[i].FindControl("Chk"));
                if (chk.Checked)
                {
                    int RoleId = int.Parse(gvRole.DataKeys[i].Value.ToString());
                    try
                    {
                        BRT.Delete(RoleId);
                        success = true;
                    }
                    catch
                    { }
                }
            }
            if (success == true)
            {
                Common.JShelper.JSAlert(this.Page, "", "删除角色成功！");
                InitBind();
            }
            else
            {
                Common.JShelper.JSAlert(this.Page, "", "删除角色失败！");
            }
        }

        protected void gvRole_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //查看用户
            if (e.CommandName == "ShowUser")
            {
                int id = int.Parse(e.CommandArgument.ToString());//取行号，从0开始
                DataKey key = gvRole.DataKeys[id];//从每一行取得datakey
                int RoleId = Int32.Parse(key.Value.ToString());

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>window.location.href='ShowUser.aspx?RoleId=" + RoleId + "';</script>");
            }
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRole.PageIndex = e.NewPageIndex;
            InitBind();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void search_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbRoleNameSerach.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "请输入要搜索的角色名称！");
                tbRoleNameSerach.Focus();
            }
            else
            {
                LabMS.BLL.RoleTable BRT = new LabMS.BLL.RoleTable();
                string str = "RoleName like '%" + tbRoleNameSerach.Text.Trim() + "%'";
                gvRole.DataSource = BRT.GetModelList(str);
                gvRole.DataBind();
            }
        }

        protected void gvRole_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkb = (LinkButton)e.Row.FindControl("LBshowuser");
                linkb.CommandArgument = e.Row.RowIndex.ToString();
            }
        }
    }
}
